#include "mainscene.h"
#include "ui_mainscene.h"
#include "gameconfig.h"
#include "gamecontroller.h"
#include "gamepushbutton.h"
#include <QPixmap>
#include <QPainter>
#include <QDebug>
#include <QTimer>

MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("翻转金币游戏"));
    this->setFixedSize(GAME_WIDTH, GAME_HEIGHT);
    this->setWindowIcon(QIcon(IMG_GAME_ICON));
    //
    GameController::getInstance()->initMainScene(this);

    //
    GamePushButton *btnStart = new GamePushButton(BTN_START_GAME, "");
    btnStart->setParent(this);
    btnStart->move((GAME_WIDTH - btnStart->width()) / 2,
                   GAME_HEIGHT - 150);
    connect(btnStart, &QPushButton::clicked, this, [=](){
        qDebug() << "clicked btnStart";
        GameController::getInstance()->playStartSound();
        btnStart->zoomUp();
        btnStart->zoomDown();
        QTimer::singleShot(300, this, [=](){
            //进入关卡界面
            GameController::getInstance()->hideMainScene();
            GameController::getInstance()->showLevelScene();
        });
    });
}

MainScene::~MainScene()
{
    delete ui;
}


void MainScene::on_action_quit_triggered()
{
    exit(0);
}


void MainScene::paintEvent(QPaintEvent *event)
{
    QPainter mainPaiter(this);
    QPixmap mainPixmap;
    bool ret = mainPixmap.load(IMG_BG_MAINSCENE);
    if(ret)
    {
        mainPaiter.drawPixmap(0, 0, mainPixmap);
    }
}
